package com.fizzkai.p2p.bussness.service.impl;

import com.fizzkai.p2p.bussness.domain.SystemAccount;
import com.fizzkai.p2p.bussness.domain.SystemAccountFlow;
import com.fizzkai.p2p.bussness.mapper.SystemAccountFlowMapper;
import com.fizzkai.p2p.bussness.service.ISystemAccountFlowService;
import com.fizzkai.p2p.core.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service@Transactional
public class SystemAccountFlowServiceImpl implements ISystemAccountFlowService {

    @Autowired
    private SystemAccountFlowMapper accountFlowMapper;

    @Override
    public int save(SystemAccountFlow entity) {
        return accountFlowMapper.insert(entity);
    }

    @Override
    public void createGainAccountManagementChargeFlow(SystemAccount account, BigDecimal amount) {
        String note = "收取用户借款手续费:" + amount + "元";
        int state = Constants.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE;
        createFlow(account, amount, note, state);
    }

    @Override
    public void createGainInterestManagerChargeFlow(SystemAccount account, BigDecimal amount) {
        String note = "收取投标用户 利息管理费:" + amount + "元";
        int state = Constants.SYSTEM_ACCOUNT_ACTIONTYPE_INTREST_MANAGE_CHARGE;
        createFlow(account, amount, note, state);
    }

    private void createFlow(SystemAccount account, BigDecimal amount, String note, int state) {
        SystemAccountFlow flow = new SystemAccountFlow();
        flow.setActionType(state);   //交易类型
        flow.setAmount(amount);
        flow.setUsableAmount(account.getUsableAmount());
        flow.setFreezeAmount(account.getFreezedAmount());
        flow.setNote(note);
        flow.setActionTime(new Date());
        accountFlowMapper.insert(flow);
    }


}
